//
//  WLSegmentViewController.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/16.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "WLSegmentViewController.h"


@interface WLSegmentViewController ()<UIScrollViewDelegate,WLSegmentTitleDelegate>
@end

@implementation WLSegmentViewController
#pragma mark life circle

+ (instancetype)initSegmentFatherWithChildTitleArray:(NSArray *)titleArr{
    return [[self alloc]initSegmentFatherWithChildTitleArray:titleArr];
}


- (instancetype)initSegmentFatherWithChildTitleArray:(NSArray *)titleArr{
    self = [super init];
    if (self) {
        self.titleArray = [titleArr mutableCopy];
        self.view.backgroundColor = [UIColor whiteColor];
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCommonViews];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSInteger index = scrollView.contentOffset.x / self.view.frame.size.width;
//    NSLog(@"iiiii == %ld",index);
    [self.segTitleView selecteTitleBtnGradualChangeWhenScrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / self.view.frame.size.width;
    
    [self.segTitleView changeTitleButtonPosition:scrollView];
    [self.segView showVCWithIndex:index outsideVC:self];
}


#pragma mark WLSegmentTitleDelegate
- (void)didSelectedSegmentTitle:(WLSegmentTitleView *)titleView atIndex:(NSInteger)index{
//    定位
    CGFloat offsetX = index * self.view.frame.size.width;
    [self.segView setContentOffset:CGPointMake(offsetX, 0)];
    
    //展示
    [self.segView showVCWithIndex:index outsideVC:self];
    
}


#pragma mark private
- (void)setupCommonViews{
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
}


#pragma mark getter

- (NSMutableArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc]init];;
    }
    return _titleArray;
}

- (NSMutableArray *)vcArray{
    if (!_vcArray) {
        _vcArray = [[NSMutableArray alloc]init];;
    }
    return _vcArray;
}

@end
